<?php
namespace app\flash\controller;
use think\Controller;
use think\Db;
use think\Exception;
use redis\RedisPackage;
use think\Common;
use think\Validate;

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods: POST,GET','OPTIONS');
// use common\Common;
// use common\Common;
/**
 * 深度文章
 */
class Deepness extends Controller
{
    /**
     * 功能：点赞
     */
    /*public function likecount(){
        $data = input('');//
        $validate = Validate::make([
            'id'  => 'require',
            'status'=>'require',
        ]);
        $res = $validate->check($data);
        if($res !== true){
            return json(msg(0, '', $validate->getError()));
        }
        $status         = input('status');//点赞状态 0-取消赞 1-点赞
        $cid            = input('cid');//app软件唯一标识
        $user_id        = input('user_id');//用户id
        $id        = input('id');//对应评论的id
        $create_time    = strtotime(date('Y-m-d H:i:s'));
        $pinglun = Db::table('comment')->where(['id'=>$id,'flag'=>2])->find();
        if(count($pinglun)==0){
            return json(msg(0, '', '该评论不存在,无法点赞'));
        }
        if ($status==1) {
            $arr = array(
                'deepness_id'    => $id,
                'cid'        => $cid,
                'status'=>$status,
                'user_id' =>$user_id,
                'create_time'=> $create_time
            );
            $result = Db::table('deepness_like')->insert($arr);
            if (!$result) {
                return json(msg(0, '', '点赞失败'));
            }
            $res  = Db::table('comment')->where(['id'=>$id,'flag'=>2])->setInc('like_count');
            if ($res) {
                return json(msg(1, '', '点赞成功'));
            }else{
                return json(msg(0, '', '点赞失败'));
            }
        }else{
            $data = Db::table('comment')->where(['id'=>$id,'flag'=>2])->find();
            if ($data['like_count']==0) {
                return json(msg(0, '', '不能再取消了'));
            }
            $result = Db::table('deepness_like')->where(['deepness_id'=>$id,'cid'=>$cid])->delete();
            if (!$result) {
                return json(msg(0, '', '取消失败'));
            }
            $res = Db::table('comment')->where(['id'=>$id,'flag'=>2])->setDec('like_count');
            if ($res) {
                return json(msg(1, '', '取消成功'));
            }else{
                return json(msg(0, '', '取消失败'));
            }
        }


    }*/

    public function likecount(){
        $data = input('');//
        $validate = Validate::make([
            'status'=>'require',
            'id'=>'require',
        ]);
        $res = $validate->check($data);
        if($res !== true){
            return json(msg(0, '', $validate->getError()));
        }
        $pinglun = Db::table('comment')->where(['id'=>$data['id'],'flag'=>2])->find();
        if(count($pinglun)==0){
            return json(msg(0, '', '该评论不存在,无法点赞'));
        }
        if($data['status']==1){
            $res  = Db::table('comment')->where(['id'=>$data['id'],'flag'=>2])->setInc('like_count');
            if($res){
                return json(msg(1, '', '点赞成功'));
            }else{
            return json(msg(0, '', '点赞失败'));
            }
        }else{
            if($pinglun['like_count']==0){
                return json(msg(0, '', '点赞数为0,不可取消'));
            }
            $res = Db::table('comment')->where(['id'=>$data['id'],'flag'=>2])->setDec('like_count');
            if ($res) {
                return json(msg(1, '', '取消成功'));
            }else{
                return json(msg(0, '', '取消失败'));
            }
        }

    }

    /**
     * 功能：深度评论查询
     * 时间：2019/08/16
     * 作者：张仕虎
     */
    public function findcomment(){
        $data = input('');//
        $id = $data['id'];
        //评论页码
        $page1 = isset($data['page1'])?$data['page1']:'1';
        //子孙级回复页码
        $page2 = isset($data['page2'])?$data['page2']:'1';
        $result = db('deepness_comment')
            ->alias('fc')
            ->join('user u','u.id = fc.from_id','left')
            ->where(array('message_id'=>$id,'parent_id'=>0))
            ->field('fc.id,fc.from_id,fc.message_id,fc.content,parent_id,u.id uid,u.username,u.phone')
            ->paginate(20,false,['query'=>request()->param(),'page'=>$page1])->each(function ($item,$key){
                $data = input('');//
                $id = $data['id'];
                $page2 = isset($data['page2'])?$data['page2']:'1';
                $is_zisun = db('deepness_comment')->where(array('message_id'=>$id,'parent_id'=>$item['id']))->find();
                //是否有子孙
                if($is_zisun){
                    $item['zisun'] = db('deepness_comment')
                        ->alias('fc')
                        ->join('user u','u.id = fc.from_id','left')
                        ->field('fc.id,fc.from_id,fc.message_id,fc.content,parent_id,u.id uid,u.username,u.phone')
                        ->where('message_id ='.$id.' and parent_id!=0')
                        ->paginate(20,false,['query'=>request()->param(),'page'=>$page2])->each(function ($item,$key){
                            //查询父级用户信息
                            $parent_id = $item['parent_id'];//父级评论的id
                            if($parent_id){
                                $parent_user_id = db('deepness_comment')->where('id',$parent_id)->value('from_id');
                                $item['parent_user_id'] = $parent_user_id;
                                $parent_user_info = db('user')->where('id',$parent_user_id)->field('username,phone')->find();
                                $item['parent_user_name']= $parent_user_info['username'];
                                $item['parent_user_phone']= $parent_user_info['phone'];
                            }
                            return $item;
                        });
                }
                return $item;
            });
        return json(msg(1,$result,'评论及回复查询成功'));
       
    }
    //子孙评论
    public function zisun_comments(){
        $data = input('');//
        $validate = Validate::make([
            'id'  => 'require',
        ]);
        $res = $validate->check($data);
        if($res !== true){
            return json(msg(0, '', $validate->getError()));
        }
        $id = $data['id'];
        $detail = db('deepness_comment')
            ->alias('fc')
            ->join('user u','u.id = fc.from_id','left')
            ->field('fc.id,fc.from_id,fc.message_id,fc.content,u.username,u.phone,like_count,u.picture,fc.create_time')
            ->where(array('fc.id'=>$id))
            ->find();
        $zisun = $this->getChildrenIds($detail['id']);
        $arr = explode(',',$zisun);
        array_shift($arr);
        $zisun = db('deepness_comment')
            ->alias('fc')
            ->join('user u','u.id = fc.from_id','left')
            ->field('fc.id,fc.from_id,fc.message_id,fc.content,u.username,u.phone,like_count,u.picture,fc.create_time,parent_id')
            ->where(array('fc.id'=>$arr))
            ->paginate(20,false,['query'=>request()->param()])->each(function ($item,$key){
                $item['content'] = json_decode($item['content']);
                $data = input('');//
                $id = $data['id'];
                //当不是二级评论时，显示父级信息
                $parent_id = $item['parent_id'];//父级评论的id
                if($parent_id != $id){
                    $parent_user_id = db('deepness_comment')->where('id',$parent_id)->value('from_id');
                    $item['parent']['parent_user_id'] = $parent_user_id;
                    $parent_user_info = db('user')->where('id',$parent_user_id)->field('username,phone')->find();
                    $item['parent']['parent_user_name']= $parent_user_info['username'];
                    $item['parent']['parent_user_phone']= $parent_user_info['phone'];
                    $parent = db('deepness_comment')-> where(array('id'=>$item['parent_id']))->find();
                    $item['parent']['parent_user_message'] = json_decode($parent['content']);
                }else{
                    $item['parent'] = null;
                }
                return $item;
            });
        return json(msg(1,compact('detail','zisun'),'子孙评论查询成功'));

    }
    //一级评论
    public function  first_comments(){
        $data = input('');//
        $validate = Validate::make([
            'id'  => 'require',
        ]);
        $res = $validate->check($data);
        if($res !== true){
            return json(msg(0, '', $validate->getError()));
        }
        $id = $data['id'];
        //评论页码
        $result = db('deepness_comment')
            ->alias('fc')
            ->join('user u','u.id = fc.from_id','left')
            ->where(array('message_id'=>$id,'parent_id'=>0))
            ->field('fc.id,fc.from_id,fc.message_id,fc.content,u.username,u.phone,like_count,u.picture')
            ->paginate(20,false,['query'=>request()->param()])->each(function ($item,$key){
                $data = input('');//
                $id = $data['id'];
                $item['content'] = json_decode($item['content']);
                //查询回复数
                $zisun = $this->getChildrenIds($item['id']);
                $arr = explode(',',$zisun);
                array_shift($arr);
                $item['reply_count'] = count($arr);
                return $item;
            });
        return json(msg(1,$result,'一级评论查询成功'));
    }
    //下级所有id
    function getChildrenIds ($id)
    {
        $ids = '';
        $result =db('deepness_comment')->where(array('parent_id'=>$id))->select();
        if ($result)
        {
            foreach ($result as $key=>$val)
            {
                $ids .= ','.$val['id'];
                $ids .= $this->getChildrenIds ($val['id']);
            }
        }
        return $ids;
    }
}